WorkerScript.onMessage = function(message) {
    doLoadFriends(message);
}

function doLoadFriends(message) {
    var xhr = new XMLHttpRequest;
    var url = "https://production-2.sogeoapi.com/api/follows?per_page=" + message.g.perPage + "&oauth_token=" + message.token
    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                message.model.clear();
                var response = JSON.parse(xhr.responseText);
                for (var i in response.data) {
                    var friend = response.data[i].user;
                    message.model.append({
                                  friendName: friend.name,
                                  index: i,
                                  friendLocation: friend.location != null ? friend.location : "",
                                  friendAvatarUrl: friend.avatar_pic != null ? friend.avatar_pic : "../Images/avatar-placeholder.png",
                                  friendSpots: friend.books_count
                    });
                }
                message.model.sync();
                WorkerScript.sendMessage({ 'result': 'SUCCESS', 'listState': '' });
            }
        } catch(err) {
            WorkerScript.sendMessage({ 'result': 'FAILURE', 'message': err, 'listState': '' });
        }

    }
    xhr.send();
}
